package com.jiepos.mpos.biz.workflowtrace;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.dao.AppWorkflowTraceDao;
import com.jiepos.mpos.common.entity.App_WorkFlow_TraceEntity;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 工作业务类
 * 
 * @ProjectName: [mpos1]
 * @Package com.jiepos.mpos.biz.workflowtrace
 * @author 肖志伟
 * @version 1.0
 * @since 2017年9月22日 下午7:31:55
 * @history（历次修订内容、修订人、修订时间等） @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Service
public class AppWorkflowTraceService extends BaseService{

	@Autowired
	private AppWorkflowTraceDao appWorkflowTraceDao;

	/**
	 * 获取会员认证审核详情
	 * @param custom_uuid
	 * @return
	 */
	public List<Map<String, Object>> getCustomCertificateInfomation(String custom_uuid) {
		StringBuffer sb = new StringBuffer();
		List list = new ArrayList();
		sb.append("select WORKFLOW_UUID,LOGIN_NAME,SYSTEM_TYPE,SYSTEM_KEY,STATE_OPERATE,AUDIT_COMMENTS,REC_UPD_USR_ID,"
				+ "to_char(t1.REC_UPD_TS,'yyyy-mm-dd hh24:mi:ss') as REC_UPD_TS,to_char(t1.rec_crt_ts,'yyyy-mm-dd hh24:mi:ss') as REC_CRT_TS, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.REALNAME_AUDIT_ST + "' and code_val = t1.old_state) as old_state, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.REALNAME_AUDIT_ST + "' and code_val = t1.new_state) as new_state ");
		sb.append("from " + Table.T_APP_WORKFLOW_TRACE + " t1 ");
		sb.append("where 1=1 ");
		sb.append(" and t1.system_type = 'T_APP_CUSTOM_INF'");
		sb.append(" and t1.system_key = ? ");
		list.add(custom_uuid);
		List<Map<String, Object>> map = appWorkflowTraceDao.queryForList(sb.toString(), list);
		return map;
	}
	
	/**
	 * 获取结算信息审核详情
	 * @param bankcard_uuid
	 * @return
	 */
	public List<Map<String, Object>> getAccountCertificateInfomation(String bankcard_uuid) {
		StringBuffer sb = new StringBuffer();
		List list = new ArrayList();
		sb.append("select WORKFLOW_UUID,LOGIN_NAME,SYSTEM_TYPE,SYSTEM_KEY,STATE_OPERATE,AUDIT_COMMENTS,REC_UPD_USR_ID,"
				+ "to_char(t1.REC_UPD_TS,'yyyy-mm-dd hh24:mi:ss') as REC_UPD_TS,to_char(t1.rec_crt_ts,'yyyy-mm-dd hh24:mi:ss') as REC_CRT_TS, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.REALNAME_AUDIT_ST + "' and code_val = t1.old_state) as old_state, ");
		sb.append("(select code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.REALNAME_AUDIT_ST + "' and code_val = t1.new_state) as new_state ");
		sb.append("from " + Table.T_APP_WORKFLOW_TRACE + " t1 ");
		sb.append("where 1=1 ");
		sb.append(" and t1.system_type = 'T_APP_CUSTOM_BANKCARD'");
		sb.append(" and t1.system_key = ? ");
		list.add(bankcard_uuid);
		List<Map<String, Object>> map = appWorkflowTraceDao.queryForList(sb.toString(), list);
		return map;
	}

	/**
	 * 保存工作流记录
	 * @param entity
	 * @param map
	 */
	public void saveWorkflowTrace(App_WorkFlow_TraceEntity entity,Map map){
		//以是否有uuid来区分操作
		if (!StringUtils.isBlank(entity.getWorkflow_uuid())) {
			//更新
			entity = (App_WorkFlow_TraceEntity) this.saveCommonData(entity,map,"update");
			List noChangeList = new ArrayList();
			noChangeList.add("workflow_uuid");
			noChangeList.add("rec_crt_ts");
			appWorkflowTraceDao.update(entity,noChangeList);
		}else{
			//新增
			entity = (App_WorkFlow_TraceEntity) this.saveCommonData(entity, map, null);
			entity.setWorkflow_uuid(IDFactoryUtil.getUUID());
			appWorkflowTraceDao.save(entity);
		}
	}

	public App_WorkFlow_TraceEntity getWorkflowEntity(String system_type,String system_key){
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select workflow_uuid, ")
				.append("        login_name, ")
				.append("        system_type, ")
				.append("        system_key, ")
				.append("        old_state, ")
				.append("        new_state, ")
				.append("        state_operate, ")
				.append("        audit_comments, ")
				.append("        rec_id ")
				.append("   from (select * ")
				.append("           from t_app_workflow_trace ")
				.append("          where rec_st != '0' ")
				.append("            and system_type = '"+system_type+"' ")
				.append("            and system_key = ? ")
				.append("          order by rec_crt_ts desc) ")
				.append("  where rownum <= 1 ");
		List<String> params = new ArrayList();
		params.add(system_key);
		List<Map<String,Object>> list = appWorkflowTraceDao.queryForList(sqlBuf.toString(), params);
		if(list != null && list.size() > 0){
			Map<String, Object> workflow = list.get(0);
			App_WorkFlow_TraceEntity entity = new App_WorkFlow_TraceEntity();
			entity.setWorkflow_uuid(StringUtils.objToString(workflow.get("WORKFLOW_UUID")));
			entity.setLogin_name(StringUtils.objToString(workflow.get("LOGIN_NAME")));
			entity.setSystem_type(StringUtils.objToString(workflow.get("SYSTEM_TYPE")));
			entity.setSystem_key(StringUtils.objToString(workflow.get("SYSTEM_KEY")));
			entity.setOld_state(StringUtils.objToString(workflow.get("OLD_STATE")));
			entity.setNew_state(StringUtils.objToString(workflow.get("NEW_STATE")));
			entity.setState_operate(StringUtils.objToString(workflow.get("STATE_OPERATE")));
			entity.setAudit_comments(StringUtils.objToString(workflow.get("AUDIT_COMMENTS")));
			entity.setRec_id(Long.parseLong(workflow.get("REC_ID").toString()));
			return entity;
		}else{
			return null;
		}
	}



	/**
	 * 根据关联业务主键；得到最新审核信息
	 * @param system_key
	 * @return
	 */
	public String getAuditCommentsBySystemkey(String system_key){
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" SELECT AUDIT_COMMENTS ")
				.append("            FROM (SELECT AUDIT_COMMENTS ")
				.append("                    FROM T_APP_WORKFLOW_TRACE ")
				.append("                   WHERE SYSTEM_KEY = ? ")
				.append("                   ORDER BY REC_CRT_TS DESC) ")
				.append("           WHERE ROWNUM <= 1 ");
		List<String> params = new ArrayList<>();
		params.add(system_key);
		List<Map<String,String>> list = appWorkflowTraceDao.queryForList(sqlBuf.toString(), params);
		if(list !=null && list.size() > 0 && list.get(0) != null && list.get(0).get("AUDIT_COMMENTS") != null){
			return list.get(0).get("AUDIT_COMMENTS");
		}else{
			return "";
		}
	}

}
